AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
--include('entities/base_wire_entity/init.lua')
include( 'shared.lua' )
util.PrecacheSound( "SB/Artillery2.wav" )

function ENT:Initialize()

	self.Entity:SetModel( "models/Slyfo/boxpallet.mdl" ) 
	self.Entity:SetName("Small Arms Factory")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )

	if WireAddon then
		self.Inputs = WireLib.CreateInputs( self, { "Create", "Weapon" } )
	end

	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(true)
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
	end
	self.Entity:SetKeyValue("rendercolor", "255 255 255")
	self.PhysObj = self.Entity:GetPhysicsObject()
	self.FireMode = 1
end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16 + Vector(0,0,300)
	
	local ent = ents.Create( "factory_small_arms" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	ent.SPL = ply
	
	return ent
	
end

function ENT:TriggerInput(iname, value)		
	if (iname == "Create") then
		if (value > 0) then
			self.Entity:HPFire()
		end
	elseif iname == "Weapon" then
		if value > 1 then
			self.FireMode = value
		else
			self.FireMode = 1
		end
	end
end

function ENT:PhysicsUpdate()

end

function ENT:Think()
	--self.val1 = RD_GetResourceAmount(self.Entity, "Munitions")

end

function ENT:PhysicsCollide( data, physobj )
	
end

function ENT:OnTakeDamage( dmginfo )
	
end

function ENT:Use( activator, caller )

end

function ENT:GetGunType()
local Ent = ""
if self.FireMode == 1 then
Ent = "weapon_Heavy_Pistol"
elseif self.FireMode == 2 then
Ent = "weapon_Hand_Cannon"
elseif self.FireMode == 3 then
Ent = "weapon_Assault_Smg"
elseif self.FireMode == 4 then
Ent = "weapon_Assault_Rifle"
else
Ent = "weapon_Heavy_Smg"
end
return Ent
end

function ENT:HPFire()
	local CDown = self.Entity:GetNetworkedFloat("ReloadTime")
	if (CurTime() >= CDown) then
		local ammo = self:GetResourceAmount("Ammo")
		local energy = self:GetResourceAmount("energy")
		if (ammo >= 100 and energy>=1000) then
			self:ConsumeResource("energy", 1000)
			self:ConsumeResource("Ammo", 50)
			local Ent = self:GetGunType()
			local Pos = self.Entity:GetPos() + (self.Entity:GetUp() * 2) 
			local Ang = self.Entity:GetUp():Angle()
			self:MakeEnt(Ent,Pos,Ang)
			self.Entity:SetNetworkedFloat("ReloadTime",CurTime() + 5)		
		end
	end
end
